'use strict';
/**
 * Created by FreezeSoul on 2015/10/16.
 */
define([
        'angular',
        'jquery',
        'app/core/loader',
        "app/core/base",
        'app/modules/index/define'
    ],
    function (angular, $, plugin, base) {
        angular.module('ad.index')
            .directive('initIndexPlugin', [function () {
                return {
                    scope: {
                        chartType: '='
                    },
                    link: function ($scope, $elm, $attrs, $ctrl) {
                        var chart = null;
                        $scope.$watch('chartType', function (newValue, oldValue) {
                            if (newValue) {
                                plugin.loadPluginModule(newValue).then(function (Chart, module) {
                                    //支持新属性方式
                                    if (angular.isObject(Chart)
                                        && Chart.Chart
                                        && Chart.Chart.prototype instanceof base) {
                                        if (Chart.Property) {
                                            //指标缺省属性
                                            $scope.$parent.indexProperty = Chart.Property;
                                        }
                                        Chart = Chart.Chart;
                                    }

                                    chart = new Chart({
                                        showLoading: function () {
                                            $(".chart-render-loading").show();
                                        },
                                        hideLoading: function () {
                                            $(".chart-render-loading").hide();
                                        },
                                        addListener: function () {
                                        },
                                        removeListener: function () {
                                        },
                                        '提示': '这里是假的layout对象，控件只有在page里才会有真正的layout对象。'
                                    });
                                    var promise = chart.init($elm[0]);
                                    var done = function () {
                                        $scope.$parent.indexPlugin = chart;
                                        $scope.$parent.indexPlugin.__isEditMode = true;
                                        var loadResources = [];
                                        if (module.editorJs) {
                                            var editorJs = module.rootPath + module.path + "/" + module.editorJs;
                                            loadResources.push(editorJs);
                                        }
                                        if (module.editorCss) {
                                            var editorCss = "css!" + module.rootPath + module.path + "/" + module.editorCss;
                                            loadResources.push(editorCss);
                                        }
                                        var editorTpl = "";
                                        if (module.editorTpl) {
                                            editorTpl = module.editorTpl.indexOf("@") > -1 ? "app/modules/index/views/index-no-editor.html" : module.rootPath + module.path + "/" + module.editorTpl;
                                        }
                                        $scope.$parent.chartIsReady();
                                        if (loadResources.length > 0) {
                                            require(loadResources, function () {
                                                $scope.$parent.$apply(function () {
                                                    $scope.$parent.propertyEditorUrl = editorTpl;
                                                    $scope.$parent.propertyEditorState = "ready";
                                                });
                                            });
                                        } else {
                                            $scope.$parent.propertyEditorState = "ready";
                                        }
                                    };
                                    if (promise) {
                                        $.when(promise).then(function () {
                                            done();
                                        })
                                    } else {
                                        done();
                                    }
                                });
                            }
                        }, true);

                        var resize = function () {
                            if ($scope.$parent.indexPlugin) {
                                $scope.$parent.indexPlugin.resize();
                            }
                        };

                        $('.charts-edit-container,#content').bind('transitionend oTransitionEnd webkitTransitionEnd', resize);
                        $scope.$on('$destroy', function () {
                            $('.charts-edit-container,#content').unbind('transitionend oTransitionEnd webkitTransitionEnd', resize);
                            if (chart) {
                                chart.dispose();
                            }
                        });

                    }
                };
            }
            ]);
    });